package com.yuye.cloud.common.model;

import com.yuye.cloud.common.exception.MyException;

import java.io.Serializable;

/**
 * 接口请求统一输出
 * 
 * @author gzs
 * @version 3.0.0
 */
public class ResultUtil implements Serializable {

	private static final long serialVersionUID = 7448631589137132448L;

	/**
	 * 成功输出,带结果
	 *
	 * @param object T类型
	 * @param <T>    泛型T
	 * @return Result Result对象
	 */
	public static <T> Result<T> success(final T object) {
		Result<T> result = new Result<T>();
		result.setCode(ResultEnum.SYS_SUCCESS.getCode());
		result.setMsg(ResultEnum.SYS_SUCCESS.getMsg());
		result.setData(object);
		return result;
	}

	/**
	 * 成功输出,带结论
	 *
	 * @param msg String类型字符串
	 * @param <T> 泛型T
	 * @return Result Result对象
	 */
	public static <T> Result success(String msg) {
		Result result = new Result();
		result.setCode(ResultEnum.SYS_SUCCESS.getCode());
		result.setMsg(msg);
		return result;
	}

	/**
	 * 成功输出,带结论
	 *
	 * @param msg    String类型字符串
	 * @param object T类型对象
	 * @param <T>    泛型T
	 * @return Result Result对象
	 */
	public static <T> Result success(String msg, final T object) {
		Result result = new Result();
		result.setCode(ResultEnum.SYS_SUCCESS.getCode());
		result.setData(object);
		result.setMsg(msg);
		return result;
	}

	/**
	 * 既有成功也有失败的，带数据
	 *
	 * @param msg    String类型字符串
	 * @param object T类型对象
	 * @param <T>    泛型T
	 * @return Result Result对象
	 */
	public static <T> Result info(String msg, final T object) {
		Result result = new Result();
		result.setCode(ResultEnum.SYS_INFO.getCode());
		result.setData(object);
		result.setMsg(msg);
		return result;
	}

	/**
	 * 失败,带msg
	 *
	 * @param msg String类型字符串
	 * @param <T> 泛型T
	 * @return Result对象
	 */
	public static <T> Result error(String msg) {
		Result result = new Result();
		result.setCode(ResultEnum.SYS_ERROR.getCode());
		result.setMsg(msg);
		return result;
	}
	

	/**
	 * 成功输出,不带结果
	 *
	 * @return Result对象
	 */
	public static Result success() {
		Result result = new Result();
		result.setCode(ResultEnum.SYS_SUCCESS.getCode());
		result.setMsg(ResultEnum.SYS_SUCCESS.getMsg());
		return result;
	}

	/**
	 * 错误输出不带结果
	 *
	 * @return Result对象
	 */
	public static Result error() {
		Result result = new Result();
		result.setCode(ResultEnum.SYS_ERROR.getCode());
		result.setMsg(ResultEnum.SYS_ERROR.getMsg());
		return result;
	}

	/**
	 * 异常信息输出
	 *
	 * @param myException MyException异常对象
	 * @return Result对象
	 */
	public static Result result(MyException myException) {
		Result result = new Result();
		result.setCode(myException.getCode());
		result.setMsg(myException.getMsg());
		result.setData(myException.getMessage());
		return result;
	}

	public static void fail(String msg) {
		throw new MyException(msg);
	}

	public static Result<Object> error(ResultEnum parameterError) {
		Result result = new Result();
		result.setCode(parameterError.getCode());
		result.setMsg(parameterError.getMsg());
		return result;
	}

	public static <T> Result<T> of(Integer code, String msg, T datas) {
		  return new Result<>( code, msg, datas);
	}
}
